package org.dru.clay.util.io;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class IOUtils {
	public static void silentClose(InputStream inputStream) {
		if (inputStream == null) {
			return;
		}
		try {
			inputStream.close();
		} catch (Exception e) {
			/* ignore */
		}
	}

	public static void silentClose(OutputStream outputStream) {
		if (outputStream == null) {
			return;
		}
		try {
			outputStream.close();
		} catch (Exception e) {
			/* ignore */
		}
	}

	public static byte[] read(InputStream inputStream) {
		final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		try {
			byte[] buffer = new byte[65536];
			int bytes = 0;
			while ((bytes = inputStream.read(buffer)) >= 0) {
				outputStream.write(buffer, 0, bytes);
			}
			return outputStream.toByteArray();
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			IOUtils.silentClose(inputStream);
		}
	}
}
